/**
* 
* GUI: Graphical programme (launch console)
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* Classe: GUIprogramme.java 
* see <a href="{@docRoot}/">for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/
package programme.GUI;

import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


import javax.swing.*;
import javax.swing.border.LineBorder;

import programme.programme;
import storm.SimulationKernel;
/**
 * 
 * @author richard
 *
 */
public class GUIProgramme  extends JFrame
	{

	public JConsole Controle;
	JScrollBar areaScrollPane;
	JFrame temp;
	private String nameFile;
	InternalFrame[] frames;
	MDIPane Fond;
	public programme envt;
	Image logo;
	public InternalFrame frameConsole;
    	
/**
 * 	
 */
	   public void paint (Graphics g){
		   super.paint(g);
		    Graphics2D g2 = (Graphics2D) g;

	   }
/**
 * 
 */

	public GUIProgramme()
	  {
	    super("STORM");
	    this.setSize(700, 700);
  
	    setFocusTraversalKeysEnabled(false);
	    Fond = new MDIPane();
	    GridBagLayout layout =new GridBagLayout() ;
	    GridBagConstraints c = new GridBagConstraints();
	    c.fill = GridBagConstraints.HORIZONTAL;
	    c.anchor = GridBagConstraints.PAGE_END;
	    ImageIcon icon = null;
	    this.setLayout(layout);
	    ClassLoader cl = this.getClass().getClassLoader();
	    /**
	     * Back ground logo
	     */
	    java.net.URL url = cl.getResource("images/logo.gif");
	    icon = new ImageIcon(url,"");
	    JLabel label = new JLabel("storm.rts-software.org", icon, JLabel.LEFT);
	    label.setVerticalTextPosition(JLabel.BOTTOM);
	    label.setHorizontalTextPosition(JLabel.CENTER);
	    /**
	     * 	Console 
	     */
	    this.getContentPane().add(label,c);
        frameConsole = new InternalFrame ("Console");
	    frameConsole.setBounds(100, 400, 505, 200);
        Controle = new JConsole("",20,60);
	    Controle.setFrame(this);
	    Controle.setFont(new Font("Courier", Font.PLAIN, 14));
	    JScrollPane areaScrollPane = new JScrollPane(Controle);
	    areaScrollPane.setVerticalScrollBarPolicy(
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	      areaScrollPane.setPreferredSize(new Dimension(250, 155));
	      areaScrollPane.setMinimumSize(new Dimension(10, 10));
        frameConsole.getContentPane().add(  areaScrollPane, BorderLayout.CENTER);
        getLayeredPane().add(frameConsole);
	      
	    WindowListener l = new WindowAdapter() {
	      public void windowClosing(WindowEvent e) {
	        System.exit(0);
	      }
	    };
	    addWindowListener( new WindowAdapter() {
	        public void windowOpened( WindowEvent e ){
	        	Controle.setCaretPosition(4);
	            Controle.requestFocus();
	          }
	        } );
	    Dimension dim = getToolkit().getScreenSize();
	    setLocation(dim.width/2-getWidth()/2, 
	      dim.height/2-getHeight()/2);
	    url = cl.getResource("images/icon.gif");
	    ImageIcon image = new ImageIcon(url);
	    setIconImage(image.getImage());
	    addWindowListener(l);
	    setVisible(true);
	  }

	/**
	 * 	@deprecated
	 */
	public void setError() {
		
	}


	



}
/**
 * private class manage layout
 * @author richard
 *
 */
class MDIPane extends JLayeredPane implements ComponentListener
{
	private Image logo;
	/**
	 * 
	 */
	public void paint(Graphics g) {
		super.paint(g);
	    
	  }
	/**
	 * 
	 */
    public MDIPane() {
 
      addComponentListener(this);
      setOpaque(true);
     }
  /**
   * 
   */
  public void componentHidden(ComponentEvent e) {}
  public void componentMoved(ComponentEvent e) {}
  public void componentShown(ComponentEvent e) {}
  public void componentResized(ComponentEvent e) { lineup(); }
  /**
   * 
   */
  public void lineup() {
    int frameHeight, frameWidth, currentX, currentY, lheight, lwidth;
    lwidth = getWidth();
    lheight = getHeight();
    currentX = 0;
    currentY = lheight;
    Component[] components = getComponents();
    for (int i=components.length-1; i>-1; i--) {
      if (components[i] instanceof InternalFrame) {
    	  InternalFrame tempFrame = (InternalFrame) components[i];
        frameHeight = tempFrame.getHeight();
        frameWidth = tempFrame.getWidth();
        if (tempFrame.isMaximized()) {
          tempFrame.setBounds(0,0,getWidth(),getHeight());
          tempFrame.validate(); 
          tempFrame.repaint(); 
        }
        else if (tempFrame.isIconified()) {
          if (currentX+frameWidth > lwidth) {
            currentX = 0;
            currentY -= frameHeight;
          }
          tempFrame.setLocation(currentX, currentY-frameHeight);
          currentX += frameWidth;
        }
      }
    }
  }
}
  
  
